const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'

Page({
  data: {
    motto: 'Hello World',
    userInfo: {
      avatarUrl: defaultAvatarUrl,
      nickName: '',
    },
    hasUserInfo: false,
    canIUseGetUserProfile: wx.canIUse('getUserProfile'),
    canIUseNicknameComp: wx.canIUse('input.type.nickname'),
    categories: [
      { name: '潮玩', img: '/pages/category/toy.png' },
      { name: '服装', img: 'https://images.unsplash.com/photo-1512436991641-6745cdb1723f?auto=format&fit=crop&w=400&q=80' },
      { name: '饰品', img: 'https://images.unsplash.com/photo-1517841905240-472988babdf9?auto=format&fit=crop&w=400&q=80' },
      { name: '农具', img: 'https://images.unsplash.com/photo-1506744038136-46273834b3fb?auto=format&fit=crop&w=400&q=80' },
      { name: '汽车', img: 'https://images.unsplash.com/photo-1503736334956-4c8f8e92946d?auto=format&fit=crop&w=400&q=80' },
      { name: '代金券', img: '/pages/category/djj.png' },
      { name: '学生', img: 'https://images.unsplash.com/photo-1513258496099-48168024aec0?auto=format&fit=crop&w=400&q=80' },
      { name: '美妆', img: '/pages/category/mz.png' },
      { name: '母婴', img: 'https://images.unsplash.com/photo-1508214751196-bcfd4ca60f91?auto=format&fit=crop&w=400&q=80' },
      { name: '数码', img: 'https://images.unsplash.com/photo-1518770660439-4636190af475?auto=format&fit=crop&w=400&q=80' },
      { name: '日用', img: '/pages/category/ry.png' },
      { name: '洗护', img: '/pages/category/xh.png' },
    ],
    menuList: ['图书期刊', '文化用品', '音像内容'],
    currentMenu: 0,
    subData: [
      {
        title: '图书期刊',
        children: ['绘本', '小说', '漫画', '教材', '教辅', '报纸', '考公资料', '英语读物', '四六级', '课外读物']
      },
      {
        title: '文化用品',
        children: ['卡册', '显微镜', '笔', '书法用品', '手帐', '墨水瓶', '胶带', '计算器', '笔袋']
      },
      {
        title: '音像内容',
        children: ['专辑', '唱片', '动画片', 'cd', '综艺', '短剧', '电影', '电视剧', '纪录片']
      }
    ],
    subList: []
  },
  bindViewTap() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onChooseAvatar(e) {
    const { avatarUrl } = e.detail
    const { nickName } = this.data.userInfo
    this.setData({
      "userInfo.avatarUrl": avatarUrl,
      hasUserInfo: nickName && avatarUrl && avatarUrl !== defaultAvatarUrl,
    })
  },
  onInputChange(e) {
    const nickName = e.detail.value
    const { avatarUrl } = this.data.userInfo
    this.setData({
      "userInfo.nickName": nickName,
      hasUserInfo: nickName && avatarUrl && avatarUrl !== defaultAvatarUrl,
    })
  },
  getUserProfile(e) {
    wx.getUserProfile({
      desc: '展示用户信息',
      success: (res) => {
        console.log(res)
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    })
  },
  goHome() {
    wx.redirectTo({
      url: '/pages/index/index'
    })
  },
  goMine() {
    wx.redirectTo({
      url: '/pages/mine/index'
    })
  },
  onCategoryTap(e) {
    const idx = e.currentTarget.dataset.index;
    const name = this.data.categories[idx].name;
    if (name === '潮玩') {
      wx.navigateTo({ url: '/pages/toy/index' })
    } else if (name === '农具') {
      wx.navigateTo({ url: '/pages/tools/index' })
    } else if (name === '饰品') {
      wx.navigateTo({ url: '/pages/jewelry/index' })
    }
  },
  onLoad() {
    this.setData({ subList: [this.data.subData[0]] })
  },
  onMenuTap(e) {
    const idx = e.currentTarget.dataset.index
    this.setData({
      currentMenu: idx,
      subList: [this.data.subData[idx]]
    })
  },
  onSubTap(e) {
    // 点击子分类，跳转到详情页（可根据实际需求传递参数）
    wx.navigateTo({ url: '/pages/goodsDetail/goodsDetail' });
  },
}) 